package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 广告位管理
 * </p>
 *
 * @author JiangYunTian
 * @since 2022-10-11
 */
@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {
    @Autowired
    private IYddAdvposService yddAdvposService;

    @Autowired
    private IYddAdvimgService yddAdvimgService;

    //分页查询
    @GetMapping("/list")
    public ResultData selectByPage(String advposname, Integer page, Integer limit) {
        Page<YddAdvpos> page1 = new Page<>(page, limit);
        Page<YddAdvposDto> dtoPage = new Page<>();

        //创建条件构造器
        LambdaQueryWrapper<YddAdvpos> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(advposname), YddAdvpos::getAdvposname, advposname);
        yddAdvposService.page(page1, lqw);

        //对象拷贝,除records数据,需要重新封装添加advposnum数据
        BeanUtils.copyProperties(page1, dtoPage, "records");
        List<YddAdvpos> yddAdvposList = page1.getRecords();

        List<YddAdvposDto> yddAdvposDtoList = yddAdvposList.stream().map((item) -> {
            YddAdvposDto yddAdvposDto = new YddAdvposDto();
            BeanUtils.copyProperties(item, yddAdvposDto);

            Integer id = item.getId();

            //用id通过条件构造器查询广告图数量
            LambdaQueryWrapper<YddAdvimg> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(YddAdvimg::getAdvimgpos, id);

            Integer advposnum = yddAdvimgService.count(lqw1);

            yddAdvposDto.setAdvposnum(advposnum);

            return yddAdvposDto;
        }).collect(Collectors.toList());

        //将封装的list赋值给page,并作为data返回数据
        dtoPage.setRecords(yddAdvposDtoList);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("data", dtoPage.getRecords());

        map.put("count", (int) dtoPage.getTotal());
        map.put("totalPages", (int) dtoPage.getPages());
        map.put("pageSize", dtoPage.getSize());
        map.put("currentPage", dtoPage.getCurrent());

        return ResultData.ok("", map);
    }

    //修改
    @PostMapping("/update")
    public R update(@RequestBody YddAdvpos yddAdvpos) {
        yddAdvposService.updateById(yddAdvpos);
        return R.success("200", null);
    }

    //根据id查询(数据回显)
    @GetMapping("/rowInfo")
    public ResultData selectById(Integer id) {
        YddAdvpos byId = yddAdvposService.getById(id);
        return ResultData.ok("", byId);
    }

    //删除
    @GetMapping("/del")
    public R delete(Integer id) {
        yddAdvposService.removeById(id);
        return R.success("200", null);
    }

    //查询全部
    @GetMapping("/all")
    public ResultData selectAll() {
        List<YddAdvpos> list = yddAdvposService.list();
        return ResultData.ok("", list);
    }

    //添加
    @PostMapping("/add")
    public R add(@RequestBody YddAdvpos yddAdvpos) {
        yddAdvposService.save(yddAdvpos);
        return R.success("200", null);
    }
}
